using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wall : MonoBehaviour
{
    public List<Vector3> keyPoints=new List<Vector3>();
    public Vector3 MovePoints;
    List<Vector3> allPoints;
    public float Radius;
    public float High;
    public MeshFilter meshFilter;
    public MeshRenderer meshRenderer;
    public bool BeginCreate=false;

    public Vector3[] MyVertices;
    public int[] MyTriangles;


    public void AddKeyPoint(Vector3 vector)
    {
        keyPoints.Add(vector);
        Debug.Log(keyPoints.Count);
    }
    public void Create()
    {
        List<Vector3> keyPointSelf = keyPoints;
        //if (BeginCreate)
        //{
        //    keyPointSelf.Add(MovePoints);
        //}
        int length = keyPointSelf.Count;
        if (length >= 2)
        {
            allPoints = new List<Vector3>();
            for (int i = 0; i < length; i++)
            {
                if (i == 0)
                {
                    Vector3 beginPoint = (keyPointSelf[0] - keyPointSelf[1]) + keyPointSelf[0];
                    allPoints.AddRange(GetAreaPoint(beginPoint, keyPointSelf[0], keyPointSelf[1]));
                }
                else if (i == length - 1)
                {
                    Vector3 endPoint = (keyPointSelf[i] - keyPointSelf[i - 1]) + keyPointSelf[i];
                    allPoints.AddRange(GetAreaPoint(keyPointSelf[i - 1], keyPointSelf[i], endPoint));
                }
                else
                {
                    allPoints.AddRange(GetAreaPoint(keyPointSelf[i - 1], keyPointSelf[i], keyPointSelf[i + 1]));
                }
            }
            meshFilter.mesh = CreateMesh(allPoints);
        }

    }
    public void CreateHigh()
    {
        meshFilter.mesh = CreateHighMesh(allPoints);
    }
    Vector3[] GetAreaPoint(Vector3 Up ,Vector3 Self,Vector3 Down)
    {
        Vector3[] vector3s = new Vector3[2];
        Vector3 vecZUp = new Vector3(0, 1, 0);
        Vector3 vecZDown = new Vector3(0, -1, 0);
        Vector3 vecOne = Vector3.Normalize(Up- Self) -Vector3.Normalize(Down- Self);

        Vector3 vecOneUpN = Vector3.Normalize(Vector3.Cross(vecOne, vecZUp));
        Vector3 vecOneDownN = Vector3.Normalize(Vector3.Cross(vecOne, vecZDown));
        float angle = Vector3.Angle(Up - Self, Down - Self)*0.5f;
        float length = Radius / Mathf.Sin(Mathf.Deg2Rad* angle);

        vector3s[0] = Self + (vecOneUpN * length);
        vector3s[1] = Self + (vecOneDownN * length);
        return vector3s;

    }

    Mesh CreateMesh(List<Vector3> allPoints)
    {
        Mesh mesh = new Mesh();
        MyVertices = allPoints.ToArray();
        mesh.vertices = allPoints.ToArray();

        MyTriangles = GetTriangles(allPoints);
        mesh.triangles = MyTriangles;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        return mesh;
    }
    int[] GetTriangles(List<Vector3> allPoints)
    {
        List<int> triangles = new List<int>();
        int length = allPoints.Count;
        for (int i = 0; i < length-2; i+=2)
        {
            triangles.Add(i);
            triangles.Add(i+1);
            triangles.Add(i+3);
            triangles.Add(i);
            triangles.Add(i+3);
            triangles.Add(i+2);
        }
        return triangles.ToArray();
    }

    Mesh CreateHighMesh(List<Vector3> allPoints)
    {
        Vector3 high = new Vector3(0, High, 0);
        Mesh mesh = new Mesh();
        int length = allPoints.Count;
        Vector3[] vectors = new Vector3[length * 2];
        for (int i = 0; i < length; i++)
        {
            vectors[i] = allPoints[i] + high;
            vectors[i + length] = allPoints[i];
        }
        mesh.vertices = vectors;
        mesh.triangles = GetHighTriangles(vectors);
        mesh.uv = GetUV(vectors);
        //mesh.RecalculateNormals();
        //mesh.RecalculateBounds();
        return mesh;
    }
    Vector2[] GetUV(Vector3[] allPoints)
    {
        int length = allPoints.Length;
        int half = length / 2;
        Vector2[] vector2s = new Vector2[length];
        float dis=0;
        for (int i = 0; i < length-2; i+=2)
        {
            if (i<half)
            {
                if (i==0)
                {
                    vector2s[i] = new Vector2(1, dis);
                    vector2s[i+1] = new Vector2(1, Vector3.Distance(allPoints[0], allPoints[1]));
                }
                else if (i == half-2)
                {
                    dis += Vector3.Distance(allPoints[i], allPoints[i + 2]);
                    vector2s[i] = new Vector2(1, dis);
                    vector2s[i+1] = new Vector2(1, dis+ Vector3.Distance(allPoints[i], allPoints[i + 1]));
                }
                else
                {
                    dis += Vector3.Distance(allPoints[i], allPoints[i + 2]);
                    vector2s[i] = new Vector2(1,dis);
                    vector2s[i+1] = new Vector2(1, dis);
                }
            }
            else
            {
                if (i == half)
                {
                    dis = 0;
                    vector2s[i] = new Vector2(0,dis);
                    vector2s[i + 1] = new Vector2(0, Vector3.Distance(allPoints[0], allPoints[1]));
                }
                else if (i == length - 2)
                {
                    dis += Vector3.Distance(allPoints[i], allPoints[i + 2]);
                    vector2s[i] = new Vector2(0, dis);
                    vector2s[i + 1] = new Vector2(0, dis + Vector3.Distance(allPoints[i], allPoints[i + 1]));
                }
                else
                {
                    dis += Vector3.Distance(allPoints[i], allPoints[i + 2]);
                    vector2s[i] = new Vector2(0,dis);
                    vector2s[i + 1] = new Vector2(0,dis);
                }
            }
        }
        return vector2s;
    }
    int[] GetHighTriangles(Vector3[] allPoints)
    {
        List<int> triangles = new List<int>();
        int length = allPoints.Length;
        int half = length / 2;
        for (int i = 0; i < half - 2; i += 2)
        {
            triangles.Add(i);
            triangles.Add(i + 1);
            triangles.Add(i + 3);
            triangles.Add(i);
            triangles.Add(i + 3);
            triangles.Add(i + 2);
        }

        triangles.Add(half);
        triangles.Add(0);
        triangles.Add(1);
        triangles.Add(half);
        triangles.Add(half+1);
        triangles.Add(1);

        for (int i = half; i < length-2; i++)
        {
            triangles.Add(i);
            triangles.Add(i - half);
            triangles.Add(i - half + 2);
            triangles.Add(i);
            triangles.Add(i - half + 2); 
            triangles.Add(i + 2);
        }

        triangles.Add(length-2);
        triangles.Add(length-half - 2);
        triangles.Add(length - half - 1);
        triangles.Add(length - 2);
        triangles.Add(length - 1);
        triangles.Add(length - half - 1);

        return triangles.ToArray();
    }
}
